package ai.people.netmon.framework.domain.userconfig.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * （监测系统用户）配置采集关键词表
 * </p>
 *
 * @author zhenglin
 * @since 2022-02-28
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
public class UserCollectConfigKeywords implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * id
     */
    @TableId(value = "id", type = IdType.AUTO)
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 采集关键词
     */
    @NotBlank(message = "crawlKeywords不能为空")
    private String crawlKeywords;
    /**
     * 信源类别
     */
    @NotBlank(message = "infoType不能为空")
    private String infoType;

    /**
     * 采集状态
     */
    private String collectionStatus;

    /**
     * 采集频次，预留值，秒级
     */
    private String crawlInterval;

    /**
     * 用户唯一标识
     */
    private String userId;

    /**
     * 配置来源，（monitor，warning）
     */
    private String appName;

    /**
     * 合作伙伴ID 格式 [xx,cc]
     */
    private String partnerId;

    public static final String INFO_SPLIT = ",";
    public static final String KEYWORDS_SPLIT = "\\|";



    /**
     * 获取按 , 分隔的infoType 集合
     *
     * @return {@link List}<{@link String}>
     */
    public List<String> getInfoTypeSplit() {
        if (StringUtils.isBlank(this.getInfoType())) {
            return Collections.emptyList();
        }
        return Arrays.asList(this.getInfoType().split(INFO_SPLIT));
    }


    /**
     * 获取两个集合中减少的infoTpe集合
     *
     * @param data 数据
     * @return {@link List}<{@link String}>
     */
    public List<String> getInfoTypeLess(UserCollectConfigKeywords data) {
        return getLess(this.getInfoTypeSplit(),data.getInfoTypeSplit());
    }

    /**
     * 获取两个集合中增加的infoTpe集合
     *
     * @param data 数据
     * @return {@link List}<{@link String}>
     */
    public List<String> getInfoTypeMore(UserCollectConfigKeywords data) {
        return getMore(this.getInfoTypeSplit(),data.getInfoTypeSplit());
    }

    private List<String> getLess(List<String> s1,List<String> s2){
        return s1.stream().filter(x -> !s2.contains(x)).collect(Collectors.toList());
    }

    private List<String> getMore(List<String> s1,List<String> s2){
        return getLess(s2,s1);
    }

    /**
     * 获得抓取关键词分割
     *
     * @return {@link List}<{@link String}>
     */
    public List<String> getCrawlKeywordsSplit() {
        if (StringUtils.isBlank(this.crawlKeywords)) {
            return Collections.emptyList();
        }
        return Arrays.asList(this.crawlKeywords.split(KEYWORDS_SPLIT));
    }

    /**
     * 获取两个集合中减少的CrawlKeywords集合
     *
     * @param data 数据
     * @return {@link List}<{@link String}>
     */
    public List<String> getCrawlKeywordsLess(UserCollectConfigKeywords data) {
        return getLess(this.getCrawlKeywordsSplit(),data.getCrawlKeywordsSplit());
    }

    /**
     * 获取两个集合中增加的CrawlKeywords集合
     *
     * @param data 数据
     * @return {@link List}<{@link String}>
     */
    public List<String> getCrawlKeywordsMore(UserCollectConfigKeywords data) {
        return getMore(this.getCrawlKeywordsSplit(),data.getCrawlKeywordsSplit());
    }


    public List<UserCollectConfigKeywords> buildSyncData(){
        if (StringUtils.isBlank(this.crawlKeywords) || StringUtils.isBlank(this.getInfoType())){
            return Collections.emptyList();
        }
        List<String> infoTypeSplit = getInfoTypeSplit();

        List<UserCollectConfigKeywords> keywords = new ArrayList<>();
        for (String info : infoTypeSplit) {
            List<String> crawlKeywordsSplit = getCrawlKeywordsSplit();
            for (String keyword : crawlKeywordsSplit) {
                UserCollectConfigKeywords newKeywords = new UserCollectConfigKeywords();
                newKeywords.setId(this.id);
                newKeywords.setCrawlKeywords(keyword);
                newKeywords.setInfoType(info);
                newKeywords.setCrawlInterval(this.crawlInterval);
                newKeywords.setUserId(this.userId);
                newKeywords.setAppName(this.appName);
                newKeywords.setCollectionStatus(this.getCollectionStatus());
                newKeywords.setPartnerId(this.partnerId);
                keywords.add(newKeywords);
            }
        }
        return keywords;
    }

    public void setInfoTypeFromList(List<String> list){
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        this.setInfoType(String.join(",",list));
    }

    public void setCrawlKeywordsFromList(List<String> list){
        if (CollectionUtils.isEmpty(list)){
            return;
        }
        this.crawlKeywords = String.join(",",list);
    }
}
